package com.alibaba.citrus.cr.mn.order.center.facade.function.convert;

import com.alibaba.citrus.cr.common.constant.SaleOrderConstants;
import com.alibaba.citrus.cr.common.util.DateUtils;
import com.alibaba.fastjson.JSONObject;
import com.epoch.app.bcorder.platform_enhance.SaleOrderTag;
import com.epoch.app.crb2btradecenter.domain.order.model.Order;
import com.epoch.app.mnordercenter.model.dto.OrderSDO;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;

/**
 * @author zhoubc
 * @date 2023/5/28 12:05
 */
public class OrderToOrderSDOConvertor {
    //调拨单业务ID
    private static final String ALLOCATE_ORDER_BIZ_ID = "allocateOrderBizId";
    //调拨单ID
    private static final String ALLOCATE_ORDER_ID = "allocateOrderId";

    public static OrderSDO convert(Order order) {
        OrderSDO orderSDO = new OrderSDO();
        orderSDO.setId(String.valueOf(order.getId()));
        orderSDO.setOrderBizId(order.getOrderBizId());
        orderSDO.setOuterOrderId(order.getOuterOrderId());
        orderSDO.setOrderType(order.getOrderType());
        orderSDO.setOrderTypeName(order.getOrderTypeName());
        if (null == order.getOrderTypeName() && null != order.getOrderType()) {
            orderSDO.setOrderTypeName(SaleOrderTag.of(order.getOrderType()).getDesc());
        }

        orderSDO.setOrderTag(order.getOrderTag());
        if (null != order.getStatus()) {
            orderSDO.setOrderStatus(String.valueOf(order.getStatus()));
        }
        orderSDO.setCustomerCode(order.getCustomerCode());
        orderSDO.setCustomerName(order.getCustomerName());
        orderSDO.setReceiveAddressCode(order.getReceiveAddressCode());
        orderSDO.setReceiveAddressName(order.getReceiveDetailAddress());
        orderSDO.setAreaCode(order.getAreaCode());
        orderSDO.setBisUnitCode(order.getBisUnitCode());
        orderSDO.setBisUnitName(order.getBisUnitName());
        orderSDO.setSalesOrganization(order.getOrganizationCode());
        orderSDO.setSalesOrganizationName(order.getOrganizationName());
        orderSDO.setSalesChannel(order.getSaleChannelCode());
        orderSDO.setSalesChannelName(order.getSaleChannelName());
        orderSDO.setSalesDepartment(order.getDepartmentCode());
        orderSDO.setSalesDepartmentName(order.getDepartmentName());
        orderSDO.setBizChannel(order.getChannelCode());
        orderSDO.setBizChannelName(order.getChannelName());
        orderSDO.setCostCenter(order.getCostCenter());
        orderSDO.setCashCustomerName(order.getCashCustomerName());
        orderSDO.setCustomerReferenceNo(order.getCustomerReferenceNo());
        if (null != order.getCustomerExpectDate()) {
            orderSDO.setCustomerExpectDate(DateUtils.toDateStr(order.getCustomerExpectDate(),DateUtils.DATE_FORMAT));
        }
        orderSDO.setProductGroupCode(order.getProductGroupCode());
        orderSDO.setProductGroupName(order.getProductGroupName());
        orderSDO.setSalesmanId(order.getSalesmanId());
        orderSDO.setSalesmanName(order.getSalesmanName());

        if (null != order.getGmtCreate()) {
            orderSDO.setGmtCreate(DateUtils.toDateStr(order.getGmtCreate(),DateUtils.DATETIME_FORMAT));
        }
        if (null != order.getGmtModified()) {
            orderSDO.setGmtModified(DateUtils.toDateStr(order.getGmtModified(),DateUtils.DATETIME_FORMAT));
        }

        String creator = order.getCreator();
        if (StringUtils.isNotBlank(creator)){
            JSONObject jsonObject = JSONObject.parseObject(creator);
            String empId = jsonObject.getString("empId");
            if (StringUtils.isNotBlank(empId)&&empId.startsWith("CZ_")){
                //截取CZ_前缀
                String bigIntEmpId = empId.substring(3);
                orderSDO.setCreatorId(bigIntEmpId);
            }
        }
        orderSDO.setCreator(order.getCreatorName());
        orderSDO.setUpdator(order.getModifierName());
        orderSDO.setCreateReason(order.getCreateReason());
        orderSDO.setCreateReasonName(order.getCreateReasonName());//查字典
        orderSDO.setDeliveryMethod(order.getTransportModeCode());
        orderSDO.setDeliveryMethodName(order.getTransportModeName());//查字典
        orderSDO.setContact(order.getContact());
        orderSDO.setContactPhone(order.getContactPhone());
        orderSDO.setOrderTotalFee(order.getOrderTotalFee());
        orderSDO.setFrontOrderId(order.getFrontOrderId());
        orderSDO.setFrontOrderBizId(order.getPreOrderNo());
        orderSDO.setShippingWarehouseCode(order.getWareHouseOutCode());
        orderSDO.setShippingWarehouseName(order.getWareHouseName());
        orderSDO.setReceiveWareHouseCode(order.getReceiveWareHouseOutCode());
        orderSDO.setReceiveWareHouseName(order.getReceiveWareHouseName());
        orderSDO.setCustomerAccounts(order.getCustomerAccounts());
        orderSDO.setBusinessType(order.getBusinessType()!=null?String.valueOf(order.getBusinessType()):null);
        orderSDO.setBusinessTypeName(order.getBusinessTypeName());
        orderSDO.setRemark(order.getRemark());
        orderSDO.setFeatures(order.getFeatures());
        if (StringUtils.isNotBlank(order.getFeatures())) {
            com.alibaba.fastjson.JSONObject jsonObject = JSONObject.parseObject(order.getFeatures());
            orderSDO.setAllocateOrderId(jsonObject.getString(ALLOCATE_ORDER_ID));
            orderSDO.setAllocateOrderBizId(jsonObject.getString(ALLOCATE_ORDER_BIZ_ID));

            if (StringUtils.isBlank(orderSDO.getFrontOrderBizId())
                    && jsonObject.getString(SaleOrderConstants.FRONT_ORDER_BIZ_ID) != null) {
                orderSDO.setFrontOrderBizId(jsonObject.getString(SaleOrderConstants.FRONT_ORDER_BIZ_ID));
            }
        }

        orderSDO.setSyncVersion(order.getSyncVersion());
        return orderSDO;
    }

}
